<template>
  <div>
    <el-form :model="condition" size="small" :inline="true">
      <el-form-item label="巡检任务名称">
        <el-input v-model="condition.name" placeholder="请输入巡检任务名称" clearable />
      </el-form-item>
      <el-form-item label="巡检任务编号">
        <el-input v-model="condition.lineId" placeholder="请输入巡检任务编号" clearable />
      </el-form-item>
      <el-form-item label="巡检任务起始编号">
        <el-input v-model="condition.startNumber" placeholder="请输入巡检任务起始编号" clearable />
      </el-form-item>
      <el-form-item label="巡检任务结束编号">
        <el-input v-model="condition.endNumber" placeholder="请输入巡检任务结束编号" clearable />
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="condition.state" placeholder="请选择审核状态" clearable>
          <el-option v-for="(s,index) in auditStates" :key="index" :label="s" :value="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="巡检员">
        <el-input v-model="condition.inspectionUser" placeholder="请输入巡检员" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset()">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" @click="showInspectionDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 34px">
      <el-pagination background align="right" v-show="total > 0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="pageSizes"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next"
                     :total="total">
      </el-pagination>
    </div>
    <!-- el-table的data属性就是用来绑定这个表格显示的数据 -->
    <el-table :data="inspections" height="250" style="width: 100%">
      <!-- el-table-column表示表格的每一列信息 label属性表示表头的名称
         prop表示绑定的字段名 width属性表示这一列显示的宽度-->
      <el-table-column prop="id" label="巡检任务编号"/>
      <el-table-column prop="name" label="巡检任务名称"/>
      <el-table-column prop="lineId" label="巡检线路编号" />
      <el-table-column prop="startNumber" label="巡检任务起始杆号" />
      <el-table-column prop="endNumber" label="巡检任务结束杆号" />
      <el-table-column prop="state" label="审核状态">
        <template slot-scope="scope">
          <span v-if="scope.row.state === 0">待分配</span>
          <span v-else-if="scope.row.state === 1">已分配</span>
          <span v-else-if="scope.row.state === 2">执行中</span>
          <span v-else>已完成</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="inspectionUser" label="巡检员" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="showInspectionDialog(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteInspection(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="`${inspectionDialog.isUpdate ? '编辑巡检任务' : '新增巡检任务'}`" :visible.sync="inspectionDialog.visible" width="50%">
      <el-form :model="inspectionDialog.editInspection" :rules="inspectionDialog.inspectionRules" ref="inspectionForm" :inline="true">
        <el-form-item label="巡检任务编号" v-show="inspectionDialog.isUpdate">
          <el-input v-model="inspectionDialog.editInspection.id" readonly/>
        </el-form-item>
        <el-form-item prop="name" label="巡检任务名称">
          <el-input v-model="inspectionDialog.editInspection.name" placeholder="请输入任务名称" clearable />
        </el-form-item>
        <el-form-item prop="lineId" label="巡检线路编号">
          <el-input v-model="inspectionDialog.editInspection.lineId" placeholder="请输入巡检线路编号" clearable />
        </el-form-item>
        <el-form-item prop="startNumber" label="巡检任务起始编号">
          <el-input v-model="inspectionDialog.editInspection.startNumber" placeholder="巡检任务起始编号" clearable />
        </el-form-item>
        <el-form-item prop="endNumber" label="巡检任务结束编号">
          <el-input v-model="inspectionDialog.editInspection.endNumber" placeholder="巡检任务结束编号" clearable />
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="inspectionDialog.editInspection.remark" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-form-item prop="state" label="审核状态" v-show="inspectionDialog.isUpdate">
          <el-select v-model="inspectionDialog.editInspection.state" placeholder="请选择审核状态" clearable>
            <el-option v-for="(s,index) in auditStates" :key="index" :label="s" :value="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="inspectionUser" label="巡检员">
          <el-input v-model="inspectionDialog.editInspection.inspectionUser" placeholder="请输入巡检员" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="save">保 存</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "inspectionTask",
  data(){
    return {
      condition: {
        name: '',
        lineId:'',
        state: '',
        startNumber:'',
        endNumber:'',
        inspectionUser: ''
      },
      auditStates: ['待分配', '已分配', '执行中', '已完成'],
      inspections: [],
      currentPage: 1, //分页的默认页码应该是第一页
      pageSizes: [50, 100, 150, 200, 400], //可选的每页显示条数
      pageSize: 50, //每页显示条数
      total: 0, //查询的总条数，默认值为0，查询之后才会赋值
      inspectionDialog: {
        visible: false,
        isUpdate: false,
        editInspection: {
          id: '',
          name: '',
          lineId:'',
          startNumber:'',
          endNumber:'',
          state: '',
          inspectionUser: '',
          remark: ''
        },
        inspectionRules: {
          name: [
            {required: true, message: '任务名称不能为空', trigger: ['blur', 'change']}
          ],
          lineId: [
            {required: true, message: ' 巡检线路编号不能为空', trigger: ['blur', 'change']}
          ],
          inspectionUser: [
            {required: true, message: '巡检员不能为空', trigger: ['blur', 'change']}
          ]
        }
      }
    }
  },
  methods: {
    handleSizeChange(val){//这个函数是用来监听每页显示条数的变更
      this.currentPage = 1
      this.pageSize = val
      this.search()
    },
    handleCurrentChange(val){//这个函数是用来监听当前页码变化
      this.currentPage = val
      this.search()
    },
    search(){
      this.doGet('/inspectionTask', {
        name: this.condition.name,
        lineId:this.condition.lineId,
        state: this.condition.state,
        startNumber:this.condition.startNumber,
        endNumber:this.condition.endNumber,
        inspectionUser: this.condition.inspectionUser,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }, resp => {
        this.inspections = resp.records
        this.total = resp.total
      })
    },
    showInspectionDialog(inspection){
      if(inspection){
        this.inspectionDialog.isUpdate = true
        //首先获取user对象的属性集合，然后遍历这个集合，将每一个属性值拷贝到editInspection对象中
        Object.keys(inspection).forEach(key => this.inspectionDialog.editInspection[key] = inspection[key])
      } else {
        this.inspectionDialog.isUpdate = false
      }
      this.inspectionDialog.visible = true
    },
    closeDialog(){
      this.inspectionDialog.visible = false
      this.inspectionDialog.editinspection = {
        id: '',
        name: '',
        lineId:'',
        startNumber:'',
        endNumber:'',
        state: '',
        inspectionUser: '',
        remark: ''
      }
      //重置所有的表单验证，这样可以清除提示信息
      this.$refs['inspectionForm'].resetFields();
    },
    save(){
      this.$refs['inspectionForm'].validate(valid => {
        if(valid){
          if(this.inspectionDialog.isUpdate){
            //这里就可以提交信息去后台服务器保存数据
            this.doPut('/inspectionTask', this.inspectionDialog.editInspection, resp => {
              if(resp){
                this.$message.success("保存成功")
                this.closeDialog()
                this.search()
              } else {
                this.$message.error("保存失败")
              }
            })
          } else {
            this.doPost('/inspectionTask', this.inspectionDialog.editInspection, resp => {
              if(resp){
                this.$message.success("保存成功")
                this.closeDialog()
                this.search()
              } else {
                this.$message.error("保存失败")
              }
            })
          }
        } else {
          return false
        }
      })
    },
    deleteInspection(inspection){
      this.showConfirm(`确定要删除任务${inspection.id}吗?`, () => {
        //点击确定按钮后才会执行删除动作
        this.doDelete(`/inspectionTask/${inspection.id}`, {}, resp  => {
          if(resp){
            this.$message.success("删除成功")
            this.search()
          } else {
            this.$message.error("删除失败")
          }
        })
      })
    },
    reset(){
      this. condition = {
        name: '',
        lineId:'',
        state: '',
        startNumber:'',
        endNumber:'',
        inspectionUser: ''
      }
      this.search()
    }
  },
  mounted() { //这个函数是一个钩子函数，有vue自动调用，在页面完成挂载之后就会调用这个函数
    this.search()
  }
}
</script>

<style scoped>

</style>
